﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using AutoGear.BLL;
using Ext.Net;
using ECommerce.Library.DataStructure;

public partial class admin_inventory_report : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
            Session["PrintOrderList"] = null;
        }
    }
    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindOrders(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);

    }

    private void BindOrders(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<Inventory> data = null;
        int totalCount = 0;
        // start = start * limit;
        if (String.IsNullOrEmpty(sort))
            sort = "InventoryId desc";
        data = Inventory.GetBackendListByCondition(string.Empty, sort, start, limit);
        totalCount = Inventory.GetBackendListByConditionCount(string.Empty);
        Store1.DataSource = data;
        Store1.DataBind();
        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateInventory(int id, string qty)
    {
        qty = qty.Trim();
        
        //ProductPurchaseOrder productPurchaseOrder = ProductPurchaseOrder.Load(id);
        Inventory inventory = Inventory.Load(id);
        int quantity=Convert.ToInt32(qty);
        if (!string.IsNullOrEmpty(qty))
        {
            try
            {

                inventory.QTY =quantity;
                inventory.CostValue = inventory.Price * quantity;
                inventory.Update();
                Store1.DataBind();
            }
            catch
            {
                X.MessageBox.Notify("Failure", "Failed to update");
            }
        }
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void ExportExcell()
    {
        Session["report"] = "Inventory";
        Response.Redirect(SiteGlobals.BaseUrl + "admin/DownloadCSV.aspx");
    }
    [DirectMethod(Namespace = "Polana.WebService")]
    public void InsertNew()
    {
        Inventory.DeleteInventory();
        Inventory.InsertInventory();
        Store1.DataBind();
    }
}